package jsyf.oa.web.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExportJXExcel;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.RegionAchivementsFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by y on 2018/8/2.
 */
@Api(value = "初始化数据接口", description = "初始化数据接口")
@Controller
@RequestMapping("/regionAchivementsService")
public class RegionAchivementsController {
    @Autowired
    private RegionAchivementsFacade regionAchivementsFacade;
    @RequestMapping(value = "/initData.json", method = RequestMethod.POST)
    public void selectByBrandId(HttpServletResponse response) {
        String result = this.regionAchivementsFacade.initData();
        if (!StringUtils.isBlank(result)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @RequestMapping(value = "/initDataByUserId.json", method = RequestMethod.POST)
    public void initDataByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        String result = this.regionAchivementsFacade.initDataByUserId(jsonData);
        if (!StringUtils.isBlank(result)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @RequestMapping(value = "/UpdateSaleAndPush.json", method = RequestMethod.POST)
    public void UpdateSaleAndPush(HttpServletResponse response) {
        String result = this.regionAchivementsFacade.countCurrentMoney();
        if (!StringUtils.isBlank(result)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/countCurrentMoney.json", method = RequestMethod.POST)
    public void countCurrentMoney(HttpServletResponse response) {
        String result = this.regionAchivementsFacade.countCurrentMoney();
        if (!StringUtils.isBlank(result)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @ResponseBody
    @RequestMapping(value = "/initLzData.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "离职人员数据",
            notes = "离职人员数据 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'month':'2018-08'}", dataType = "String", paramType = "body")
    public void initLzData(HttpServletResponse response, @RequestBody String jsonData) {
        String result = this.regionAchivementsFacade.initLzData(jsonData);
        if (!StringUtils.isBlank(result)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @RequestMapping(value = "/transferPosCountMoney.json", method = RequestMethod.POST)
    public void transferPosCountMoney(HttpServletResponse response, @RequestBody String jsonData) {
        String result = this.regionAchivementsFacade.transferPosCountMoney(jsonData);
        if (!StringUtils.isBlank(result)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @RequestMapping(value = "/deliveryCountMoney.json", method = RequestMethod.POST)
    public void DeliveryCountMoney(HttpServletResponse response, @RequestBody String jsonData) {
        String result = this.regionAchivementsFacade.deliveryCountMoney(jsonData);
        if (!StringUtils.isBlank(result)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @RequestMapping(value = "/initDataByCurrentMonth.json", method = RequestMethod.POST)
    public void initDataByCurrentMonth(HttpServletResponse response) {
        String result = this.regionAchivementsFacade.initDataByCurrentMonth();
        if (!StringUtils.isBlank(result)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @RequestMapping(value = "/getSale.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取销售记录",
            notes = "获取销售记录",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getSale(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.regionAchivementsFacade.getSaleList(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/getManagerSale.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取总经理销售记录",
            notes = "获取总经理销售记录",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getManagerSale(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.regionAchivementsFacade.getManagerSaleList(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }
    @RequestMapping(value = "/getDownByCondition.json")
    public void getDownByCondition(HttpServletResponse response, @RequestParam String companyId, @RequestParam String dateTime, @RequestParam String roleName, @RequestParam String selectCompanyId) throws Exception {
        String jsonData="{\"companyId\":\""+companyId+"\",\"dateTime\":\"" +dateTime + "\",\"roleName\":\"" +roleName+"\"}";
        String jsonData2="{\"companyId\":\""+selectCompanyId+"\",\"dateTime\":\"" +dateTime + "\"}";
        JSONObject result=regionAchivementsFacade.getManagerSaleList(jsonData);
        JSONObject result2 = this.regionAchivementsFacade.getSaleList(jsonData2);
        List<JSONObject> ManagerList = (List<JSONObject>) net.sf.json.JSONArray.toCollection(net.sf.json.JSONArray.fromObject(result.getString("records")), JSONObject.class);
        List<JSONObject> ManagerList2 = (List<JSONObject>) net.sf.json.JSONArray.toCollection(net.sf.json.JSONArray.fromObject(result2.getString("records")), JSONObject.class);
        List<JSONObject> newManagerList=new ArrayList<JSONObject>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int Month = Integer.parseInt(dateTime.split("-")[1]);
        String title="销售绩效考核报表"+"("+dateTime+")";
        //拼接标题前两行
        System.out.println(jsonData);
        JSONObject th=new JSONObject();
        th.put("companyName","公司");
        th.put("roleName","岗位");
        th.put("regionName","区域");
        th.put("userName","负责人");
        th.put("cycle","考核周期");
        th.put("targetNum","目标");
        th.put("finishNum","完成");
        th.put("saleReward","奖励金额");
        th.put("salePunishment","罚款金额");
        th.put("total","奖惩合计");
        th.put("remark","目标完成情况");
        newManagerList.add(th);
        for(JSONObject object:ManagerList){
            newManagerList.add(object);
        }
        System.out.println(ManagerList2.size());
        for(JSONObject object:ManagerList2){
            newManagerList.add(object);
        }
        export(title, newManagerList,response);
    }
    public void export(String title, List<JSONObject> ManagerList,HttpServletResponse response) throws Exception {
        ExportJXExcel excel = new ExportJXExcel(title, ManagerList,response);
        excel.creat2003Excel();
    }

}
